<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是数据结构——栈的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV03-08-数据结构-栈">
<meta property="og:url" content="https://sumumm.github.io/post/2dee0fea.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是数据结构——栈的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-08-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E6%A0%88/img/image-20220430092434203.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-08-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E6%A0%88/img/image-20220430092600384.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-08-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E6%A0%88/img/image-20220430094437528.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-08-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E6%A0%88/img/image-20220430094855574.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-08-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E6%A0%88/img/image-20220430134519127.png">
<meta property="article:published_time" content="2022-04-25T01:43:06.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.991Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV03-数据结构">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-08-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E6%A0%88/img/image-20220430092434203.png">


<link rel="canonical" href="https://sumumm.github.io/post/2dee0fea.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/2dee0fea.html","path":"post/2dee0fea.html","title":"LV03-08-数据结构-栈"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV03-08-数据结构-栈 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E6%A0%88%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-text">一、栈的概念</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E9%A1%BA%E5%BA%8F%E6%A0%88"><span class="nav-text">二、顺序栈</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%AD%98%E5%82%A8%E7%BB%93%E6%9E%84"><span class="nav-text">1.存储结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%BB%93%E6%9E%84%E4%BD%93%E5%AE%9E%E7%8E%B0"><span class="nav-text">2.结构体实现</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E9%A1%BA%E5%BA%8F%E6%A0%88%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="nav-text">3.顺序栈基本操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E5%88%9B%E5%BB%BA%E9%A1%BA%E5%BA%8F%E6%A0%88"><span class="nav-text">3.1创建顺序栈</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2%E6%A0%88%E7%8A%B6%E6%80%81%E6%A3%80%E6%B5%8B"><span class="nav-text">3.2栈状态检测</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-1%E6%A0%88%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA"><span class="nav-text">3.2.1栈是否为空</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-2%E6%A0%88%E6%98%AF%E5%90%A6%E5%B7%B2%E6%BB%A1"><span class="nav-text">3.2.2栈是否已满</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3%E6%95%B0%E6%8D%AE%E5%85%A5%E6%A0%88"><span class="nav-text">3.3数据入栈</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4%E6%95%B0%E6%8D%AE%E5%87%BA%E6%A0%88"><span class="nav-text">3.4数据出栈</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5%E6%B8%85%E7%A9%BA%E6%A0%88"><span class="nav-text">3.5清空栈</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-6%E9%87%8A%E6%94%BE%E9%A1%BA%E5%BA%8F%E6%A0%88"><span class="nav-text">3.6释放顺序栈</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-7%E6%98%BE%E7%A4%BA%E9%A1%BA%E5%BA%8F%E6%A0%88"><span class="nav-text">3.7显示顺序栈</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E9%93%BE%E5%BC%8F%E6%A0%88"><span class="nav-text">三、链式栈</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%AD%98%E5%82%A8%E7%BB%93%E6%9E%84-1"><span class="nav-text">1.存储结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%BB%93%E6%9E%84%E4%BD%93%E5%AE%9E%E7%8E%B0-1"><span class="nav-text">2.结构体实现</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E9%93%BE%E5%BC%8F%E6%A0%88%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="nav-text">3.链式栈的基本操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E9%93%BE%E6%A0%88%E5%88%9B%E5%BB%BA"><span class="nav-text">3.1链栈创建</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2%E9%93%BE%E6%A0%88%E9%87%8A%E6%94%BE"><span class="nav-text">3.2链栈释放</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3%E9%93%BE%E6%A0%88%E6%98%BE%E7%A4%BA"><span class="nav-text">3.3链栈显示</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4%E9%93%BE%E6%A0%88%E7%8A%B6%E6%80%81"><span class="nav-text">3.4链栈状态</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5%E6%95%B0%E6%8D%AE%E5%85%A5%E6%A0%88"><span class="nav-text">3.5数据入栈</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-6%E6%95%B0%E6%8D%AE%E5%87%BA%E6%A0%88"><span class="nav-text">3.6数据出栈</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/2dee0fea.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV03-08-数据结构-栈 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV03-08-数据结构-栈
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-04-25 09:43:06" itemprop="dateCreated datePublished" datetime="2022-04-25T09:43:06+08:00">2022-04-25</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="url" rel="index"><span itemprop="name">LV03-数据结构</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>3.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>13 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是数据结构——栈的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-08-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E6%A0%88/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、栈的概念"><a href="#一、栈的概念" class="headerlink" title="一、栈的概念"></a><font size=3>一、栈的概念</font></h1><p>栈是限制在一端进行插入操作和删除操作的<strong>线性表</strong>（俗称<strong>堆栈</strong>）。</p>
<ul>
<li><p><strong>栈顶</strong>：允许进行操作的一端称为栈顶。</p>
</li>
<li><p><strong>栈底</strong>：另一固定端称为栈底。</p>
</li>
<li><p><strong>空栈</strong>：没有元素的栈称为空栈。</p>
</li>
<li><p><strong>出栈</strong>：栈中提取出指定元素，此过程被称为出栈（或弹栈）。</p>
</li>
<li><p><strong>入栈</strong>：向栈中添加元素，此过程被称为进栈（入栈或压栈）。</p>
</li>
</ul>
<p>根据栈的定义，可以想象，水杯可以想象为一个栈，接水的时候相当于元素入栈，倒水的时候相当于元素出栈，没有水的时候就相当于空栈。</p>
<p>栈的特点是<strong>后进先出</strong>（ LIFO ），它是一种特殊的线性存储结构，顺序结构和链式结构都可以实现栈，使用顺序结构实现的栈称为顺序栈，使用链式结构实现的栈称为链式栈。两种实现方式的区别仅限于数据元素在实际物理空间上存放的相对位置，顺序栈底层采用的是数组，链栈底层采用的是链表。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-08-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E6%A0%88/img/image-20220430092434203.png" alt="image-20220430092434203" style="zoom:33%;" />

<h1 id="二、顺序栈"><a href="#二、顺序栈" class="headerlink" title="二、顺序栈"></a><font size=3>二、顺序栈</font></h1><h2 id="1-存储结构"><a href="#1-存储结构" class="headerlink" title="1.存储结构"></a><font size=3>1.存储结构</font></h2><p>顺序栈，即用顺序表实现栈存储结构。顺序表（底层实现是数组）和栈的存储数据的方式非常想似，只不过栈对数据的存取过程有特殊的限制，而顺序表没有。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-08-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E6%A0%88/img/image-20220430092600384.png" alt="image-20220430092600384" style="zoom:33%;" />

<h2 id="2-结构体实现"><a href="#2-结构体实现" class="headerlink" title="2.结构体实现"></a><font size=3>2.结构体实现</font></h2><p>可以把顺序栈看做是顺序表的一种，具有顺序表同样的存储结构，由数组定义，配合用数组下标表示的栈顶指针 top （相对指针）完成各种操作。顺序栈数据类型定义的 C 语言实现如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="type">int</span> <span class="type">data_t</span>; <span class="comment">/* 定义栈中数据元素的数据类型 */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> </span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">data_t</span> *data;  <span class="comment">/* 用指针指向栈的存储空间 */</span></span><br><span class="line">	<span class="type">int</span> maxlen;    <span class="comment">/* 当前栈的最大元素个数 */</span></span><br><span class="line">	<span class="type">int</span> top;       <span class="comment">/* 指示栈顶位置(数组下标)的变量 */</span></span><br><span class="line">&#125;seqstack;         <span class="comment">/* 顺序栈类型定义 */</span></span><br></pre></td></tr></table></figure>

<h2 id="3-顺序栈基本操作"><a href="#3-顺序栈基本操作" class="headerlink" title="3.顺序栈基本操作"></a><font size=3>3.顺序栈基本操作</font></h2><p>相关的源代码可以在这里下载：</p>
<table>
    <tr>
        <td align="center" width=150>数据结构</td>
        <td align="left"><a href="https://wwz.lanzouy.com/b036ygw9g" target="_blank">栈  <i class="fa fa-external-link-alt"></i> </a>  (密码:du8k)</td>  
    </tr>
</table>


<h3 id="3-1创建顺序栈"><a href="#3-1创建顺序栈" class="headerlink" title="3.1创建顺序栈"></a><font size=3>3.1创建顺序栈</font></h3><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-08-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E6%A0%88/img/image-20220430094437528.png" alt="image-20220430094437528" style="zoom:33%;" />

<details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqstack_create</span></span><br><span class="line"><span class="comment"> * @Description: 顺序栈创建</span></span><br><span class="line"><span class="comment"> * @param len: 顺序栈的最大长度 </span></span><br><span class="line"><span class="comment"> * @return  : 返回一个地址</span></span><br><span class="line"><span class="comment"> *            NULL,创建失败;</span></span><br><span class="line"><span class="comment"> *            other,创建成功</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">seqstack * <span class="title function_">seqstack_create</span><span class="params">(<span class="type">int</span> len)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 定义一个顺序栈结构体变量 */</span></span><br><span class="line">	seqstack * S;</span><br><span class="line">	<span class="comment">/* 1.申请栈空间 */</span></span><br><span class="line">	<span class="keyword">if</span> ((S =(seqstack *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(seqstack))) == <span class="literal">NULL</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;seqstack malloc failed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2.申请栈中元素数据空间 */</span></span><br><span class="line">	<span class="keyword">if</span> ((S-&gt;data = (<span class="type">data_t</span> *)<span class="built_in">malloc</span>(len * <span class="keyword">sizeof</span>(<span class="type">data_t</span>)))==<span class="literal">NULL</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;seqstack data malloc data failed！\n&quot;</span>);</span><br><span class="line">		<span class="built_in">free</span>(S); <span class="comment">/* 申请失败后，释放前边申请的栈空间 */</span></span><br><span class="line">		<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/* 3.申请的内存空间位于堆上，需要进行初始化 */</span></span><br><span class="line">	<span class="built_in">memset</span>(S-&gt;data, <span class="number">0</span>, len * <span class="keyword">sizeof</span>(<span class="type">data_t</span>));</span><br><span class="line">	S-&gt;maxlen = len;</span><br><span class="line">	S-&gt;top = <span class="number">-1</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> S;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-2栈状态检测"><a href="#3-2栈状态检测" class="headerlink" title="3.2栈状态检测"></a><font size=3>3.2栈状态检测</font></h3><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-08-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E6%A0%88/img/image-20220430094855574.png" alt="image-20220430094855574" style="zoom:33%;" />

<h4 id="3-2-1栈是否为空"><a href="#3-2-1栈是否为空" class="headerlink" title="3.2.1栈是否为空"></a><font size=3>3.2.1栈是否为空</font></h4><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqstack_empty</span></span><br><span class="line"><span class="comment"> * @Description: 顺序栈是否为空</span></span><br><span class="line"><span class="comment"> * @param s : 顺序栈地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,顺序栈非空;</span></span><br><span class="line"><span class="comment"> *            1,顺序栈为空;</span></span><br><span class="line"><span class="comment"> *            -1,顺序栈不存在</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">seqstack_empty</span><span class="params">(seqstack * S)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1.判断栈空间是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span> (S == <span class="literal">NULL</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;seqstack is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/* 2.判断栈的状态 */</span></span><br><span class="line">	<span class="keyword">if</span>(S-&gt;top == <span class="number">-1</span>) </span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="comment">/* 或者写成下边的 */</span></span><br><span class="line">	<span class="comment">//return (s-&gt;top == -1 ? 1 : 0); </span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h4 id="3-2-2栈是否已满"><a href="#3-2-2栈是否已满" class="headerlink" title="3.2.2栈是否已满"></a><font size=3>3.2.2栈是否已满</font></h4><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqstack_full</span></span><br><span class="line"><span class="comment"> * @Description: 顺序栈是否已满</span></span><br><span class="line"><span class="comment"> * @param s : 顺序栈地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,顺序栈未满;</span></span><br><span class="line"><span class="comment"> *            1,顺序栈已满;</span></span><br><span class="line"><span class="comment"> *            -1,顺序栈不存在</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">seqstack_full</span><span class="params">(seqstack * S)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1.判断栈空间是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span> (S == <span class="literal">NULL</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;seqstack is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/* 2.判断栈的状态 */</span></span><br><span class="line">	<span class="keyword">if</span>(S-&gt;top == S-&gt;maxlen<span class="number">-1</span>) </span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="comment">/* 或者写成下边的 */</span></span><br><span class="line">	<span class="comment">//return (s-&gt;top == -1 ? 1 : 0); </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-3数据入栈"><a href="#3-3数据入栈" class="headerlink" title="3.3数据入栈"></a><font size=3>3.3数据入栈</font></h3><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqstack_push</span></span><br><span class="line"><span class="comment"> * @Description: 入栈</span></span><br><span class="line"><span class="comment"> * @param S : 顺序栈地址</span></span><br><span class="line"><span class="comment"> * @param value : 要入栈的数据</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,释放成功;</span></span><br><span class="line"><span class="comment"> *            -1,顺序栈不存在或者栈已满</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">seqstack_push</span><span class="params">(seqstack * S, <span class="type">data_t</span> value)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1.判断栈空间是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span> (S == <span class="literal">NULL</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;seqstack is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2.判断栈空间是否已满 */</span></span><br><span class="line">	<span class="keyword">if</span> (S-&gt;top == S-&gt;maxlen<span class="number">-1</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;seqstack is full!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 3.更新数据 */</span></span><br><span class="line">	S-&gt;top++;</span><br><span class="line">	S-&gt;data[S-&gt;top] = value;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-4数据出栈"><a href="#3-4数据出栈" class="headerlink" title="3.4数据出栈"></a><font size=3>3.4数据出栈</font></h3><p>【注意】这里出栈的时候不要做栈是否存在以及是否为空的判断，这两个判断若是有返回值的话程序会认为返回值就是出栈的元素，所以栈的存在性以及是否为空，要在使用出栈函数前进行判断。</p>
<details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqstack_push</span></span><br><span class="line"><span class="comment"> * @Description: 出栈</span></span><br><span class="line"><span class="comment"> * @param S : 顺序栈地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个栈中数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">data_t</span> <span class="title function_">seqstack_pop</span><span class="params">(seqstack * S)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1. 判断栈空间为否为空 */</span></span><br><span class="line">	<span class="comment">/* 这一步不需要，因为栈中元素可能是任意数值，所以这里做判断并不合适，需要在函数外判断 */</span></span><br><span class="line">	<span class="comment">/* 2. 数据出栈 */</span></span><br><span class="line">	S-&gt;top--;</span><br><span class="line">	<span class="keyword">return</span> (S-&gt;data[S-&gt;top+<span class="number">1</span>]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-5清空栈"><a href="#3-5清空栈" class="headerlink" title="3.5清空栈"></a><font size=3>3.5清空栈</font></h3><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqstack_clear</span></span><br><span class="line"><span class="comment"> * @Description: 顺序栈清空</span></span><br><span class="line"><span class="comment"> * @param s : 顺序栈地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,顺序栈清空成功;</span></span><br><span class="line"><span class="comment"> *            -1,顺序栈不存在</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">seqstack_clear</span><span class="params">(seqstack * S)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1.判断栈空间是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span> (S == <span class="literal">NULL</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;seqstack is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">/* 2.清空栈，可以说是认为没有数据，那么它就没有数据 */</span></span><br><span class="line">	S-&gt;top = <span class="number">-1</span>; </span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-6释放顺序栈"><a href="#3-6释放顺序栈" class="headerlink" title="3.6释放顺序栈"></a><font size=3>3.6释放顺序栈</font></h3><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqstack_free</span></span><br><span class="line"><span class="comment"> * @Description: 释放顺序栈空间及数据空间</span></span><br><span class="line"><span class="comment"> * @param S : 顺序栈地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,释放成功;</span></span><br><span class="line"><span class="comment"> *            -1,顺序栈不存在 </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">seqstack_free</span><span class="params">(seqstack * S)</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1.判断顺序栈空间是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span> (S == <span class="literal">NULL</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;seqstack is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2.释放顺序栈中所有元素空间 */</span></span><br><span class="line">	<span class="keyword">if</span> (S-&gt;data != <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">free</span>(S-&gt;data);</span><br><span class="line">	&#125;	</span><br><span class="line">	<span class="comment">/* 3.释放顺序栈结构体变量空间 */</span></span><br><span class="line">	<span class="built_in">free</span>(S);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;seqstack free completed!\n&quot;</span>);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-7显示顺序栈"><a href="#3-7显示顺序栈" class="headerlink" title="3.7显示顺序栈"></a><font size=3>3.7显示顺序栈</font></h3><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: seqstack_show</span></span><br><span class="line"><span class="comment"> * @Description: 顺序栈数据显示</span></span><br><span class="line"><span class="comment"> * @param s : 顺序栈地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,顺序栈显示成功;</span></span><br><span class="line"><span class="comment"> *            -1,顺序栈不存在或者顺序栈为空</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">seqstack_show</span><span class="params">(seqstack * S)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">	<span class="comment">/* 1.判断栈空间是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span> (S == <span class="literal">NULL</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;seqstack is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 判断顺序栈是否为空 */</span></span><br><span class="line">	<span class="keyword">if</span> (S-&gt;top == <span class="number">-1</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;seqstack is empty!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 显示顺序栈元素 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;seqstack: &quot;</span>);</span><br><span class="line">	<span class="keyword">while</span>(i &lt;= S-&gt;top)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, S-&gt;data[i]);</span><br><span class="line">		i++;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">puts</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">	 </span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h1 id="三、链式栈"><a href="#三、链式栈" class="headerlink" title="三、链式栈"></a><font size=3>三、链式栈</font></h1><h2 id="1-存储结构-1"><a href="#1-存储结构-1" class="headerlink" title="1.存储结构"></a><font size=3>1.存储结构</font></h2><p>当我们固定单链表的一端，只在一段进行操作，便构成了栈，我们可以将链表头部作为栈顶的一端，这样可以避免在实现数据入栈”和 出栈 操作时做大量遍历链表的耗时操作。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/LV03-08-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E6%A0%88/img/image-20220430134519127.png" alt="image-20220430134519127" style="zoom: 33%;" />

<p>这样就意味着我们需要在头节后进行节点的插入和删除来完成入栈和出栈。</p>
<h2 id="2-结构体实现-1"><a href="#2-结构体实现-1" class="headerlink" title="2.结构体实现"></a><font size=3>2.结构体实现</font></h2><p>我们通过单链表来实现栈，这样其实它还是一个单链表，仅仅是限制了插入和删除节点的位置罢了，所以结构体可以像这样定义：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="type">int</span> <span class="type">data_t</span>; <span class="comment">/* 定义栈中数据元素的数据类型 */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">slstack_node</span> </span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">data_t</span> data;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">slstack_node</span> *<span class="title">next</span>;</span></span><br><span class="line">&#125;slstacknode, * slstacklink;</span><br></pre></td></tr></table></figure>

<h2 id="3-链式栈的基本操作"><a href="#3-链式栈的基本操作" class="headerlink" title="3.链式栈的基本操作"></a><font size=3>3.链式栈的基本操作</font></h2><p>相关的源代码可以在这里下载：</p>
<table>
    <tr>
        <td align="center" width=150>数据结构</td>
        <td align="left"><a href="https://wwz.lanzouy.com/b036ygw9g" target="_blank">栈  <i class="fa fa-external-link-alt"></i> </a>  (密码:du8k)</td>  
    </tr>
</table>


<h3 id="3-1链栈创建"><a href="#3-1链栈创建" class="headerlink" title="3.1链栈创建"></a><font size=3>3.1链栈创建</font></h3><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: slstack_create</span></span><br><span class="line"><span class="comment"> * @Description: 链式栈创建</span></span><br><span class="line"><span class="comment"> * @param   : none</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个地址</span></span><br><span class="line"><span class="comment"> *            NULL,创建失败;</span></span><br><span class="line"><span class="comment"> *            other,创建成功</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">slstacklink <span class="title function_">slstack_create</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 定义一个链式栈的结构体指针变量 */</span></span><br><span class="line">	slstacklink S;</span><br><span class="line">	<span class="comment">/* 1. 申请链式栈的内存空间 */</span></span><br><span class="line">	S = (slstacklink)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(slstacknode));</span><br><span class="line">	<span class="comment">/* 2. 判断是否申请成功 */</span></span><br><span class="line">	<span class="keyword">if</span>(S == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;slinked list stack malloc failed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 3.初始化内存空间 */</span></span><br><span class="line">	S-&gt;data = <span class="number">0</span>;</span><br><span class="line">	S-&gt;next = <span class="literal">NULL</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> S;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-2链栈释放"><a href="#3-2链栈释放" class="headerlink" title="3.2链栈释放"></a><font size=3>3.2链栈释放</font></h3><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: slstack_free</span></span><br><span class="line"><span class="comment"> * @Description: 链式栈释放</span></span><br><span class="line"><span class="comment"> * @param S : 链式栈地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个NULL</span></span><br><span class="line"><span class="comment"> *            NULL,释放完成</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">slstacklink <span class="title function_">slstack_free</span><span class="params">(slstacklink S)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 定义一个链式栈的结构体指针变量 */</span></span><br><span class="line">	slstacklink p;</span><br><span class="line">	<span class="comment">/* 1. 判断链式栈是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span> (S == <span class="literal">NULL</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;slinked stack is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 释放各个节点空间 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;slinked stack free:&quot;</span>);</span><br><span class="line">	<span class="keyword">while</span>(S != <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		p = S;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, p-&gt;data);</span><br><span class="line">		S = S-&gt;next;</span><br><span class="line">		<span class="built_in">free</span>(p);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">puts</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-3链栈显示"><a href="#3-3链栈显示" class="headerlink" title="3.3链栈显示"></a><font size=3>3.3链栈显示</font></h3><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: slstack_show</span></span><br><span class="line"><span class="comment"> * @Description: 链式栈数据显示</span></span><br><span class="line"><span class="comment"> * @param S : 链式栈地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,显示成功;</span></span><br><span class="line"><span class="comment"> *            -1,链式栈不存在</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">slstack_show</span><span class="params">(slstacklink S)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 定义一个链式栈的结构体指针变量 */</span></span><br><span class="line">	slstacklink p;</span><br><span class="line">	<span class="comment">/* 1. 判断链式栈是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span> (S == <span class="literal">NULL</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;slinked stack is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 判断链式栈是否为空 */</span></span><br><span class="line">	<span class="keyword">if</span> (S-&gt;next == <span class="literal">NULL</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;slinked stack is empty!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 3. 遍历所有节点 */</span></span><br><span class="line">	p = S;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;slinked stack:&quot;</span>);</span><br><span class="line">	<span class="keyword">while</span>(p-&gt;next != <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, p-&gt;next-&gt;data);</span><br><span class="line">		p = p-&gt;next;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">puts</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-4链栈状态"><a href="#3-4链栈状态" class="headerlink" title="3.4链栈状态"></a><font size=3>3.4链栈状态</font></h3><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: slstack_empty</span></span><br><span class="line"><span class="comment"> * @Description: 链式栈是否为空</span></span><br><span class="line"><span class="comment"> * @param S : 链式栈地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,链式栈非空;</span></span><br><span class="line"><span class="comment"> *            1,链式栈为空;</span></span><br><span class="line"><span class="comment"> *            -1,链式栈不存在</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">slstack_empty</span><span class="params">(slstacklink S)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1. 判断链式栈是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span> (S == <span class="literal">NULL</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;slinked stack is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 判断链式栈头节点后是否有节点 */</span></span><br><span class="line">	<span class="keyword">if</span>(S-&gt;next == <span class="literal">NULL</span>)</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="comment">/* 也可以写成下边的形式 */</span></span><br><span class="line">	<span class="comment">// return (S-&gt;next == NULL ? 1 : 0);</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-5数据入栈"><a href="#3-5数据入栈" class="headerlink" title="3.5数据入栈"></a><font size=3>3.5数据入栈</font></h3><details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: slstack_push</span></span><br><span class="line"><span class="comment"> * @Description: 入栈</span></span><br><span class="line"><span class="comment"> * @param S : 链式栈地址</span></span><br><span class="line"><span class="comment"> * @param value : 要入栈数据</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,入栈成功;</span></span><br><span class="line"><span class="comment"> *            -1,入栈失败</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">slstack_push</span><span class="params">(slstacklink S, <span class="type">data_t</span> value)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 定义一个链式栈的结构体指针变量 */</span></span><br><span class="line">	slstacklink p;</span><br><span class="line">	<span class="comment">/* 1. 判断链式栈是否存在 */</span></span><br><span class="line">	<span class="keyword">if</span> (S == <span class="literal">NULL</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;slinked stack is not existed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/* 2. 新建节点并初始化 */</span></span><br><span class="line">	p = (slstacklink)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(slstacknode));</span><br><span class="line">	<span class="keyword">if</span>(p == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;The new node malloc failed!\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	p-&gt;data = value;</span><br><span class="line">	p-&gt;next = <span class="literal">NULL</span>;</span><br><span class="line">	<span class="comment">/* 3.头部插入节点 */</span></span><br><span class="line">	p-&gt;next = S-&gt;next;</span><br><span class="line">	S-&gt;next = p;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-6数据出栈"><a href="#3-6数据出栈" class="headerlink" title="3.6数据出栈"></a><font size=3>3.6数据出栈</font></h3><p>【注意】这里出栈的时候不要做栈是否存在以及是否为空的判断，这两个判断若是有返回值的话程序会认为返回值就是出栈的元素，所以栈的存在性以及是否为空，要在使用出栈函数前进行判断。</p>
<details class="folding-tag" blue><summary> 点击查看函数实现 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function: slstack_pop</span></span><br><span class="line"><span class="comment"> * @Description: 出栈</span></span><br><span class="line"><span class="comment"> * @param S : 链式栈地址</span></span><br><span class="line"><span class="comment"> * @return  : 返回一个数值</span></span><br><span class="line"><span class="comment"> *            0,出栈成功;</span></span><br><span class="line"><span class="comment"> *            -1,链式栈不存在或者链式栈为空</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">data_t</span> <span class="title function_">slstack_pop</span><span class="params">(slstacklink S)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 定义一个链式栈的结构体指针变量 */</span></span><br><span class="line">	slstacklink p;</span><br><span class="line">	<span class="type">data_t</span> temp;</span><br><span class="line">	<span class="comment">/* 1. 判断链式栈是否存在 */</span></span><br><span class="line">	<span class="comment">/* 这一步不需要，因为栈中元素可能是任意数值，所以这里做判断并不合适，需要在函数外判断 */</span></span><br><span class="line">	<span class="comment">/* 2. 判断链式栈是否为空 */</span></span><br><span class="line">	<span class="comment">/* 这一步不需要，因为栈中元素可能是任意数值，所以这里做判断并不合适，需要在函数外判断 */</span></span><br><span class="line">	<span class="comment">/* 3. 出栈 */</span></span><br><span class="line">	p = S-&gt;next;</span><br><span class="line">	S-&gt;next = p-&gt;next;</span><br><span class="line">	</span><br><span class="line">	temp = p-&gt;data;</span><br><span class="line">	<span class="comment">/* 4. 释放出栈节点的内存空间 */</span></span><br><span class="line">	<span class="built_in">free</span>(p);</span><br><span class="line">	p = <span class="literal">NULL</span>;</span><br><span class="line">	<span class="keyword">return</span> temp;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>


    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/2dee0fea.html">LV03-08-数据结构-栈</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2022年04月25日 - 09:43</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/2dee0fea.html" title="LV03-08-数据结构-栈">https://sumumm.github.io/post/2dee0fea.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV03-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" rel="tag"><i class="fa fa-tag"></i> LV03-数据结构</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/ddef650e.html" rel="prev" title="LV03-09-数据结构-队列">
                  <i class="fa fa-angle-left"></i> LV03-09-数据结构-队列
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/14aefd1c.html" rel="next" title="LV03-07-数据结构-线性表-单链表其他操作">
                  LV03-07-数据结构-线性表-单链表其他操作 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
